require "wiki_lyrics/utils/strings"
require "wiki_lyrics/cli/plugins" 
require "wiki_lyrics/cli/wikilyrics" 


class Application
	def process( artist, title, album, year )
		if @featuring_fix
			artist = Strings.cleanup_artist( artist, title )
			title  = Strings.cleanup_title( title )
		end

		puts( "searching.. #{title} - #{artist}" ) 

		request = Lyrics::Request.new( artist, title, album, year )
		response, response_plugin, shown_review_dialog = fetch_lyrics( request )

		if response.lyrics
			artist = response.artist ? response.artist : response.request.artist
			title = response.title ? response.title : response.request.title
			puts "found at #{response_plugin.site_name}"
			$stdout.flush
  		return response.lyrics
		else
			puts "not found...."
			$stdout.flush
			return nil
		end

	end

end

sites = ["Lyriki", "LyricWiki"]
Plugins.load_plugins( sites )

class LyricDownload

	def download(lyric)
	  @app = Application.new true, true, false
    begin
      text=@app.process(lyric.artist,lyric.title, nil, nil )
      lyric.text=text if text
      !text.nil?
    ensure
	    @app.finalize()
    end
  end
end

